package com.qqtop10.manager.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtop10.manager.model.Admin;
import com.qqtop10.manager.model.BlackList;
import com.qqtop10.manager.model.Result;
import com.qqtop10.manager.model.SelectBlackList;
import com.qqtop10.manager.service.BlackListService;
import com.qqtop10.manager.utils.DateUtils;
import com.qqtop10.manager.utils.LogHelper;

/**
 * 
 * @ClassName: AddressController
 * @Description: 黑名单管理Controller
 *
 */
@RequestMapping("/manager/blacklist/")
@Controller
public class BlackListController extends BaseController {
	@Autowired
	@Qualifier("blackListService")
	private BlackListService blackListService;

	@RequestMapping("addBlack.do")
	@ResponseBody
	public Result addBlack(HttpServletRequest request,
			HttpServletResponse response, BlackList blackList)
			throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String black_value = URLDecoder.decode(URLDecoder.decode(blackList.getBlack_value(), "UTF-8"),"UTF-8");
		String remark = URLDecoder.decode(URLDecoder.decode(blackList.getRemark(), "UTF-8"), "UTF-8");
		String creationUser = URLDecoder.decode(URLDecoder.decode(blackList.getCreation_user(), "UTF-8"),"UTF-8");
		blackList.setBlack_value(black_value);
		blackList.setRemark(remark);
		blackList.setCreation_time(DateUtils.date());
		blackList.setCreation_user(creationUser);
		int res = blackListService.insert(blackList);
		if (res > 0) {
			Admin Tempadmin = (Admin) getRequest().getSession().getAttribute(
					"Logind");
			LogHelper.Log(Tempadmin.getAccount(), "后台黑名单管理模块", "添加成功",
					LogHelper.getLocalIp(getRequest()), Tempadmin.getAccount(),
					"", true);

			return ok("操作成功");

		} else {
			return err("添加黑名单失败");
		}
	}

	@RequestMapping("updateBlack.do")
	@ResponseBody
	public Result updateBlack(HttpServletRequest request,
			HttpServletResponse response, BlackList blackList)
			throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String black_value = URLDecoder.decode(URLDecoder.decode(blackList.getBlack_value(), "UTF-8"),"UTF-8");
		String remark = URLDecoder.decode(URLDecoder.decode(blackList.getRemark(), "UTF-8"), "UTF-8");
		blackList.setBlack_value(black_value);
		blackList.setRemark(remark);
		int ret = blackListService.updateByPrimaryKey(blackList);
		if (ret > 0) {
			Admin Tempadmin = (Admin) getRequest().getSession().getAttribute(
					"Logind");
			LogHelper.Log(Tempadmin.getAccount(), "后台黑名单管理模块", "更新成功",
					LogHelper.getLocalIp(getRequest()), Tempadmin.getAccount(),
					"", true);

			return ok("更新成功");
		} else {
			return err("操作失败");
		}
	}

	@RequestMapping("deleteById.do")
	@ResponseBody
	public Result deleteBlack(int ids) {
		int ret = blackListService.deleteByPrimaryKey(ids);
		if (ret > 0) {
			Admin Tempadmin = (Admin) getRequest().getSession().getAttribute(
					"Logind");
			LogHelper.Log(Tempadmin.getAccount(), "后台黑名单管理模块", "删除成功",
					LogHelper.getLocalIp(getRequest()), Tempadmin.getAccount(),
					"", true);

			return ok("更新成功");
		} else {
			return err("操作失败");
		}
	}

	@RequestMapping("getBlackList.do")
	@ResponseBody
	public ModelMap getAll(HttpServletRequest request,
			HttpServletResponse response, SelectBlackList blacklist)
			throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		ModelMap map = new ModelMap();
		List<BlackList> list = blackListService.selectAll(blacklist);
		if (list == null || list.size() == 0) {
			list = new ArrayList<>();
			map.put("data", list);
			map.put("msg", "暂无黑名单信息");
		} else {
			map.put("data", list);
		}
		return map;
	}

	@RequestMapping("getBlackById.do")
	@ResponseBody
	public ModelMap getListById(int id) {
		ModelMap map = new ModelMap();
		BlackList data = blackListService.selectByPrimaryKey(id);
		if (data == null) {
			map.put("data", "暂无黑名单信息");
		} else {
			map.put("data", data);
		}
		return map;
	}

	@RequestMapping("getBlackByType.do")
	@ResponseBody
	public ModelMap getListByType(BlackList blacklist) {
		ModelMap map = new ModelMap();
		List<BlackList> list = blackListService.selectByType(blacklist);
		if (list == null || list.size() == 0) {
			map.put("data", "暂无黑名单信息");
		} else {
			map.put("data", list);
		}
		return map;
	}

	@RequestMapping("getBlackByValue.do")
	@ResponseBody
	public ModelMap getListByValue(String value) {
		ModelMap map = new ModelMap();
		BlackList v = blackListService.selectByValue(value);
		if (v == null) {
			map.put("data", "暂无黑名单信息");
		} else {
			map.put("data", v);
		}
		return map;
	}

	@RequestMapping("getBlackByCreationTime.do")
	@ResponseBody
	public ModelMap getBlackByCreationTime(BlackList blacklist) {
		ModelMap map = new ModelMap();
		List<BlackList> list = blackListService.selectByCreationTime(blacklist);
		if (list == null || list.size() == 0) {
			map.put("data", "暂无黑名单信息");
		} else {
			map.put("data", list);
		}
		return map;
	}

	@RequestMapping("getBlackByCreationUser.do")
	@ResponseBody
	public ModelMap getBlackByCreationUser(BlackList blacklist) {
		ModelMap map = new ModelMap();
		List<BlackList> list = blackListService.selectByCreationUser(blacklist);
		if (list == null || list.size() == 0) {
			map.put("data", "暂无黑名单信息");
		} else {
			map.put("data", list);
		}
		return map;
	}

}
